package com.qinsilk.axon.config

import com.fasterxml.jackson.databind.ObjectMapper
import org.axonframework.config.Configurer
import org.axonframework.config.ConfigurerModule
import org.axonframework.serialization.Serializer
import org.axonframework.serialization.json.JacksonSerializer
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Primary

@Configuration
class AxonConfig {

    @Bean
    @Primary
    fun axonConfigurer(): ConfigurerModule {
        return ConfigurerModule { configurer: Configurer ->
            configurer.configureSerializer { jacksonSerializer() }
            configurer.configureEventSerializer { jacksonSerializer() }
        }
    }

    @Bean
    @Primary
    fun jacksonSerializer(): Serializer {
        val objectMapper = ObjectMapper()
        return JacksonSerializer.builder().objectMapper(objectMapper).build()
    }
}
